/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeler.setup;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import fi.honkalampisaatio.lmodeller.xml.User;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileFilter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.File;

public class PlayerPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 4736085676550081285L;
	private JComboBox<User> cbPlayerNames;
	private JCheckBox chckbxActivated;
	private JCheckBox chckbxShowSettingsMenu;
	private ImagePanel pnlPlayerImage;
	private JButton btnChangePlayerImage;
	private JButton btnNewPlayer;
	private JButton btnDeletePlayer;
	private JFileChooser fc;
	

	/**
	 * Create the panel.
	 */
	public PlayerPanel() {
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("100dlu:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("100dlu:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("20px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		fc = new JFileChooser();
		fc.setCurrentDirectory(new File("./"));
		fc.setFileFilter(new FileFilter() {

			@Override
			public boolean accept(File file) {
		        if (file.isDirectory()) {
		            return true;
		        }

		        int i = file.getName().lastIndexOf('.');
		        String extension = file.getName().substring(i+1).toLowerCase();
		        if (extension != null) {
		            if (extension.equals("tiff") ||
		                extension.equals("tif") ||
		                extension.equals("gif") ||
		                extension.equals("jpeg") ||
		                extension.equals("jpg") ||
		                extension.equals("png")) {
		                    return true;
		            } else {
		                return false;
		            }
		        }

		        return false;
			}

			@Override
			public String getDescription() {
		        return "images";
			}});
		
		JLabel lblPlayerName = new JLabel("Nimi");
		lblPlayerName.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(lblPlayerName, "2, 2, right, default");
		
		cbPlayerNames = new JComboBox<User>();
		cbPlayerNames.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblPlayerName.setLabelFor(cbPlayerNames);
		cbPlayerNames.setEditable(true);
		cbPlayerNames.addActionListener(this);
		add(cbPlayerNames, "4, 2, fill, top");
		
		chckbxActivated = new JCheckBox("Aktivoitu");
		chckbxActivated.setFont(new Font("Tahoma", Font.PLAIN, 20));
		chckbxActivated.addActionListener(this);
		add(chckbxActivated, "2, 4, center, default");
		
		chckbxShowSettingsMenu = new JCheckBox("Näytä asetukset valikko");
		chckbxShowSettingsMenu.setFont(new Font("Tahoma", Font.PLAIN, 20));
		chckbxShowSettingsMenu.addActionListener(this);
		add(chckbxShowSettingsMenu, "4, 4, center, default");
		
		btnChangePlayerImage = new JButton("Vaihda kuva");
		btnChangePlayerImage.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnChangePlayerImage.addActionListener(this);
		add(btnChangePlayerImage, "2, 8, 3, 1, center, default");
		
		pnlPlayerImage = new ImagePanel();
		pnlPlayerImage.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		add(pnlPlayerImage, "2, 10, 3, 9, fill, fill");
		
		btnNewPlayer = new JButton("Uusi pelaaja");
		btnNewPlayer.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnNewPlayer.addActionListener(this);
		add(btnNewPlayer, "2, 22, center, default");
		
		btnDeletePlayer = new JButton("Poista pelaaja");
		btnDeletePlayer.setFont(new Font("Tahoma", Font.PLAIN, 20));
		btnDeletePlayer.addActionListener(this);
		add(btnDeletePlayer, "4, 22, center, default");

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnNewPlayer)
		{
			String name = JOptionPane.showInputDialog(this, "Anna pelaajan nimi", "Uusi pelaaja", JOptionPane.QUESTION_MESSAGE);
			if(name!=null && !name.isEmpty())
			{
				User newUser = new User();
				newUser.setName(name);
				newUser.setUserId(cbPlayerNames.getItemCount());
				cbPlayerNames.addItem(newUser);
				cbPlayerNames.setSelectedItem(newUser);
				if(!cbPlayerNames.isEnabled())
				{
					cbPlayerNames.setEnabled(true);
				}
			}
		}
		else if(e.getSource() == btnDeletePlayer)
		{
			User user = (User)cbPlayerNames.getSelectedItem();
			if(user!=null && JOptionPane.showConfirmDialog(this, "Poista " + user.getName(), "Poista pelaaja", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				cbPlayerNames.removeItem(user);
				if(cbPlayerNames.getItemCount()==0)
				{
					cbPlayerNames.setEnabled(false);
				}
			}
		}
		else if(e.getSource() == cbPlayerNames)
		{
			playerChanged();
		}
		else if(e.getSource() == btnChangePlayerImage)
		{
			User user = (User)cbPlayerNames.getSelectedItem();
			if(user!=null)
			{
				int rv = fc.showDialog(this, "Ok");
				if(rv==JFileChooser.APPROVE_OPTION)
				{
					pnlPlayerImage.setImage(fc.getSelectedFile().getPath());
					user.setMenuimage(pnlPlayerImage.getImage());
				}
			}
		}
		else if(e.getSource() == chckbxShowSettingsMenu)
		{
			User user = (User)cbPlayerNames.getSelectedItem();
			if(user!=null)
			{
				user.setShowSettings(chckbxShowSettingsMenu.isSelected());
			}
		}
		else if(e.getSource() == chckbxActivated)
		{
			User user = (User)cbPlayerNames.getSelectedItem();
			if(user!=null)
			{
				user.setActive(chckbxActivated.isSelected());
			}
		}
	}
	
	protected void playerChanged()
	{
		User user = (User)cbPlayerNames.getSelectedItem();
		if(user!=null)
		{
			chckbxActivated.setSelected(user.isActive());
			chckbxShowSettingsMenu.setSelected(user.getShowSettings());
			pnlPlayerImage.setImage(user.getMenuimage());
		}
		else
		{
			chckbxActivated.setSelected(false);
			chckbxShowSettingsMenu.setSelected(false);
			pnlPlayerImage.setImage(null);
		}
	}

	public void setData(ArrayList<User> users) {
		cbPlayerNames.removeAllItems();
		for(int i=0;users!=null && i<users.size();i++)
		{
			cbPlayerNames.addItem(users.get(i));
		}
		playerChanged();
	}

	public ArrayList<User> getUsers() {
		ArrayList<User> users = new ArrayList<User>();
		
		for(int i=0; i<cbPlayerNames.getItemCount(); i++)
		{
			User user = cbPlayerNames.getItemAt(i);
			user.setUserId(i);
			users.add(user);
		}
		return users;
	}

}
